package me.jessyan.mvparms.demo.util;

import android.content.Context;

import com.nianlun.libserialport.UsbSerialPortController;
import com.nianlun.libserialport.UsbSerialPortParameters;
import com.nianlun.libserialport.usbdriver.UsbSerialPort;

public class USBRsUtils {
    private static USBRsUtils instance;
    private UsbSerialPort mUsbSerialPort;
    private UsbSerialPortController mUsbSerialPortController;

    private USBRsUtils(){
    }

    public static  USBRsUtils getInstance(){
        if(instance==null){
            synchronized(USBRsUtils.class){
                if(instance==null){
                    instance=new USBRsUtils();
                }
            }
        }
        return instance;
    }

    public void init(Context mContext,UsbSerialPortController.OnUsbSerialListener onUsbSerialListener){
        mUsbSerialPortController = new UsbSerialPortController(mContext);
        mUsbSerialPortController.setUsbSerialConnectListener(onUsbSerialListener);
        mUsbSerialPortController.openSerialPort(mUsbSerialPort, new UsbSerialPortParameters(9600,8,1,0));
    }
    //全开关02，电视模式13，阅读模式14，睡眠模式15，镜前灯06，淋浴灯07，排风扇11，廊灯08，窗帘开09，窗帘关0A，纱帘开0B，纱帘关0C,勿扰17，清理19，稍后18，退房16
    public void sendUSBLightCommand(int position,boolean isopen){
        //全开关02，电视模式13，阅读模式14，睡眠模式15，镜前灯06，淋浴灯07，排风扇11，廊灯08，
        int id = 0;
        switch(position){
            case 0:
                id = 8;
                break;
            case 1://主灯
//                id = 8;
                break;
            case 2://其他
//                id = 8;
                break;
            case 3://镜前灯
                id = 6;
                break;
            case 4://淋浴灯
                id = 7;
                break;
            case 5://全开
                id = 2;
                break;
        }
        if(isopen){
            openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 0"+id+" 00 00 01 00 00 00 00");
        }else{
            openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 0"+id+" 00 00 00 00 00 00 00");
        }

    }

    public void sendUSBCurtainCommand(int action){
        //窗帘开09，窗帘关0A，纱帘开0B，纱帘关0C
        int id = 0;
        switch (action){
            case 0://窗帘关
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 0A 00 00 00 00 00 00 00");
                break;
            case 1://窗帘开
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 09 00 00 00 00 00 00 00");
                break;
            case 2://纱帘开
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 0B 00 00 00 00 00 00 00");
                break;
            case 3://纱帘关
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 0C 00 00 00 00 00 00 00");
                break;
        }

    }

    public void sendUSBModelCommand(int action){
        //勿扰17，清理19，稍后18，退房16,电视模式13，阅读模式14，睡眠模式15
        int id = 0;
        switch (action){
            case 0://勿扰
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 17 00 00 00 00 00 00 00");
                break;
            case 1://清理
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 19 00 00 00 00 00 00 00");
                break;
            case 2://稍后
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 18 00 00 00 00 00 00 00");
                break;
            case 3://退房
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 16 00 00 00 00 00 00 00");
                break;
            case 4://电视模式
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 13 00 00 00 00 00 00 00");
                break;
            case 5://阅读模式
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 14 00 00 00 00 00 00 00");
                break;
            case 6://睡眠模式15
                openSerialPort("F1 DD 10 00 00 FF FF 75 30 02 00 15 00 00 00 00 00 00 00");
                break;
        }
    }

    private void openSerialPort(String data){
        mUsbSerialPortController.sendSerialPort(HEX16String.hexStringToByte(data));
    }
}
